<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 基础表格
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                        type="primary"
                        icon="el-icon-delete"
                        class="handle-del mr10"
                        @click="delAllSelection"
                >批量删除
                </el-button>
            </div>
            <el-table
                    :data="tableData"
                    border
                    class="table"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
                    @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" width="55" align="center" ></el-table-column>
                <el-table-column prop="complainContent" label="投诉描述" align="center">
                    <template slot-scope="scope">
                        <div class="content ql-editor" v-html="scope.row.complainContent" />
                    </template>
                </el-table-column>
                <el-table-column prop="complainTime" label="投诉时间" align="center"></el-table-column>
                <el-table-column prop="chargeId" label="负责人" align="center"></el-table-column>
                <el-table-column prop="complainState" label="状态" align="center">
                    <template slot-scope="scope">
                        <span v-if="scope.row.complainState===0">未处理</span>
                        <span v-if="scope.row.complainState===1">处理中</span>
                        <span v-if="scope.row.complainState===2">已处理</span>
                    </template>
                </el-table-column>
                <el-table-column prop="reresult" label="处理结果" align="center"></el-table-column>
                <el-table-column prop="finishTime" label="解决时间" align="center"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button v-if="scope.row.complainState===2"
                                   type="text"
                                   icon="el-icon-edit"
                                   @click="handleEdit(scope.$index, scope.row)"
                        >评价
                        </el-button>
                        <el-button v-if="scope.row.complainState===0"
                                   type="text"
                                   icon="el-icon-edit"
                                   @click="handleEdit(scope.$index, scope.row)"
                        >编辑
                        </el-button>
                        <el-button v-if="scope.row.complainState===1"
                                   type="text"
                                   icon="el-icon-edit"
                                   disabled
                                   @click="handleEdit(scope.$index, scope.row)"
                        >编辑
                        </el-button>
                        <el-button
                                type="text"
                                icon="el-icon-delete"
                                class="red"
                                @click="handleDelete(scope.id, scope.row)"
                        >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination class="mpage"
                           background
                           layout="prev, pager, next"
                           :current-page="currentPage"
                           :page-size="pageSize"
                           :total="pageTotal"
                           @current-change=getData
            ></el-pagination>

        </div>
    </div>
</template>

<script>
    export default {
        components: {},
        name: 'tableComplain',
        data() {
            return {
                address: '',
                name: '',
                currentPage:1,
                pageSize:5,
                tableData: [],
                multipleSelection: [],
                delList: [],
                editVisible: false,
                pageTotal: 0,
                form: {},
                idx: -1,
                id: -1
            };
        },
        created() {
            this.getData(1);
        },
        watch:{

            '$route'(to,from){
                this.getData(1);
            }

        },

        methods: {
            //获取数据
            getData(currentPage) {
                const _this = this
                _this.$axios.post("http://localhost:8888/complain/list",{
                    params:{
                        currentPage: currentPage,
                    }
                }).then(res => {
                    //records是总数据
                    console.log(res.data.data.total)
                    _this.tableData = res.data.data.records
                    //这里是错误1：类型问题
                    _this.pageTotal=Number(res.data.data.total)
                    _this.pageSize = Number(res.data.data.size)
                    _this.currentPage = Number(res.data.data.current)
                    console.log(_this.pageTotal)
                });
            },
            // 删除操作
            handleDelete(index, row) {
                const _this = this
                // 二次确认删除
                this.$confirm(
                    '此操作将删除文件','提示',{
                        confirmButtonText: '确定删除',
                        cancelButtonText: '取消删除',
                        type: 'warning'
                    }).then(() => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    _this.$axios.get("http://localhost:8888/complain/delete",{
                        params:{
                            id: row.id,
                        }
                    }).then(res => {
                        _this.getData(_this.currentPage)
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },

            // 多选操作
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            delAllSelection() {
                const length = this.multipleSelection.length;
                let str = '';
                this.delList = this.delList.concat(this.multipleSelection);
                for (let i = 0; i < length; i++) {
                    str += this.multipleSelection[i].name + ' ';
                }
                this.$message.error(`删除了${str}`);
                this.multipleSelection = [];
            },
            // 编辑操作
            handleEdit(index, row) {
                //传参格式
                const _this = this
                //_this.$router.push("/table")
                _this.$router.push({name: "formComplain",query:{num:this.tableData[index]}})
            },

            // 分页导航
            handlePageChange(val) {
                this.$set(this, 'pageIndex', val);
                this.getData();
            }
        }
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }

    .table {
        width: 100%;
        font-size: 14px;
    }

    .red {
        color: #ff0000;
    }

    .mr10 {
        margin-right: 10px;
    }

    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }
</style>
